|
|
|
|
|
|
|
You can also use the Visual Basic VarPtr operator to obtain the address of a variable. This is shown in the following sample code: |
|
|
|
|
|
|
|
|
Private Sub Command2_Click()
Dim Y As Long
Dim myVarAddress As Long
myVarAddress = VarPtr(myVar)
Call RtlMoveMemory(Y, ByVal myVarAddress, 4)
Debug.Print "myVar Address is: " & Hex$(myVarAddress)
Debug.Print "New value of Y: " & Y
End Sub |
|
|
|
|
|
|
|
|
In this function, a new Long variable, myVarAddress, is declared. This variable is loaded with the address of the myVar variable with the help of the VarPtr function. When the myVarAddress variable is passed to the RtlMoveMemory function, it is passed with the ByVal operator. Why? Because the myVarAddress variable contains the address of the myVar variable, and we want to place that address onto the stack. When you want to place the contents of a variable on the stack, you must use the ByVal operator. This is illustrated in Figure T5-4. |
|
|
|
|
|
|
|
|
Figure T5-4:
Using a pointer variable |
|
|
|
|
|
|
|
|
As you can see, the end result of this approach is exactly the same as the previous one. Why would you want to take this more convoluted approach? |
|
|
|
|
|
|
|
|
In this particular example, you wouldn't. But then again, you're not likely to use the RtlMoveMemory function to perform an operation that can be accomplished with a simple Y = myVar statement. But there will be many situations when you are working with API functions where you will need to deal with pointers and pointer variables. And there will be many cases where you will, in fact, |
|
|
|
|
|